import router from "@/router";

// 类型
import type { Module } from "vuex";
import type { IRootState } from "../../types";
import type { ISystemState, IPagePayload } from "./type";

import {
  getPageList,
  deletePageDateById,
  newPageData,
  editPageData,
} from "@/service/main/system/system";

import LocalCache from "@/utils/cache";
import message from "@/utils/message"

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersTotalCount: 0,
      roleList: [],
      roleTotalCount: 0,
      goodsList: [],
      goodsTotalCount: 0,
      menuList: [],
      menuTotalCount: 0,
      departmentList: [],
      departmentTotalCount: 0,
      categoryList: [],
      categoryTotalCount: 0
    };
  },
  getters: {
    // 根据 类型 获取数据
    getPageListData(state) {
      return (pageName: string) => {
        const listData: any[] = (state as any)[`${pageName}List`] ?? [];
        return listData;
      };
      // return (pageName: string) => {
      //   switch (pageName) {
      //     case 'users':
      //       return state.usersList
      //       break;
      //     case 'role':
      //       return state.roleList
      //       break;
      //   }
      // }
    },
    // 获取 总页数
    pageListDataCount(state) {
      return (pageName: string) => {
        const listCount = (state as any)[`${pageName}TotalCount`] ?? 0;
        return listCount;
      };
    },
  },
  mutations: {
    changeUsersList(state, dataList: any[]) {
      state.usersList = dataList;
    },
    changeUsersCount(state, count: number) {
      state.usersTotalCount = count;
    },
    changeRoleList(state, dataList: any[]) {
      state.roleList = dataList;
    },
    changeRoleCount(state, count: number) {
      state.roleTotalCount = count;
    },
    changeGoodsList(state, dataList: any[]) {
      state.goodsList = dataList;
    },
    changeGoodsCount(state, count: number) {
      state.goodsTotalCount = count;
    },
    changeMenuList(state, dataList: any[]) {
      state.menuList = dataList;
    },
    changeMenuCount(state, count: number) {
      state.menuTotalCount = count;
    },
    changeDepartmentList(state, dataList: any[]) {
      state.departmentList = dataList;
    },
    changeDepartmentCount(state, count: number) {
      state.departmentTotalCount = count;
    },
    changeCategoryList(state, dataList: any[]) {
      state.categoryList = dataList;
    },
    changeCategoryCount(state, count: number) {
      state.categoryTotalCount = count;
    },
  },
  actions: {
    async getPageListActions({ commit }, payload: IPagePayload) {
      const pageName = payload.pageName;
      let pageUrl = `${pageName}/list`;

      // switch (pageName) {
      //   case 'users':
      //     pageUrl = `${pageName}/list`
      //     break
      //   case 'role':
      //     pageUrl = `${pageName}/list`
      //     break
      // }
      // console.log(pageUrl);

      const data = await getPageList(pageUrl, payload.queryInfo);
      if (data && data.code === 0) {
        let changePageName =
          pageName.slice(0, 1).toLocaleUpperCase() + pageName.slice(1);

        commit(`change${changePageName}List`, data.data.list);
        commit(`change${changePageName}Count`, data.data.totalCount);
        // switch (pageName) {
        //   case 'users':
        //     commit("changeUsersList", data.data.list)
        //     commit("changeUsersCount", data.data.totalCount)
        //     break
        //   case 'role':
        //     commit("changeRoleList", data.data.list)
        //     commit("changeRoleCount", data.data.totalCount)
        //     break
        // }
        // 设置本地缓存，网络请求不了
        // LocalCache.setCache("usersList", data.data.list)
        // LocalCache.setCache("usersTotalCount", data.data.totalCount)
      }
    },

    // 删除
    async deletePageDateByIdActions({ dispatch }, payload: IPagePayload) {
      const { pageName, id } = payload;
      const pageUrl = `/${pageName}/${id}`;
      const data = await deletePageDateById(pageUrl);
      if (data && data.code === 0) {
        message.success(data.data)
        dispatch("getPageListActions", {
          pageName,
        });
      } else {
        message.error(data.data)
      }
    },
    // 新建
    async createPageDataActions({ dispatch }, payload) {
      const { pageName, newData } = payload;
      const pageUrl = `/${pageName}`;
      const data = await newPageData(pageUrl, newData);
      if (data && data.code === 0) {
        message.success(data.data)
        dispatch("getPageListActions", {
          pageName,
        });
      } else {
        message.error(data.data)
      }
    },
    // 编辑
    async editPageDataActions({ dispatch }, payload) {
      const { pageName, editData, id } = payload;
      const pageUrl = `/${pageName}/${id}`;
      // await newPageData(pageUrl)
      const data = await editPageData(pageUrl, editData);
      if (data && data.code === 0) {
        message.success(data.data)
        dispatch("getPageListActions", {
          pageName,
        });
      } else {
        message.error(data.data)
      }



    },
  },
};

export default systemModule;
